﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MetroFramework.Controls;

namespace ComUIControl
{
    public partial class SizePutinControl : MetroUserControl
    {
        private string _sizeName = "Photo";
        public string SizeName
        {
            get { return _sizeName; }
            set 
            {
                labelSizeName.Text = value;
                _sizeName = value; 
            }
        }


        /// <summary>
        /// 是否启用自动比例约束
        /// </summary>
        private bool _bSelfRestraintScale = false;
        [DefaultValue(false)]
        public bool SelfScaleRestraint
        {
            get
            { 
                return _bSelfRestraintScale; 
            }
            set
            {
                _bSelfRestraintScale = value; 
            }
        }

        private Size _value;
        public Size Value
        {
            get 
            { 
                return _value; 
            }
            set 
            {
                textBoxHeight.Text = value.Height.ToString();
                textBoxWidth.Text = value.Width.ToString();
                _value = value;               
            }            
        }

        private Size _scaleSize = new Size(3,4);
        public Size ScaleSize
        {
            get
            {
                return _scaleSize;
            }
            set
            {
                _scaleSize = value;
            }
        }
        public SizePutinControl()
        {
            InitializeComponent();
            SizeName = _sizeName;
        }

        private void metroTextBoxWidth_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\b' 
                || Char.IsDigit(e.KeyChar) 
                || Char.IsControl(e.KeyChar)
                || e.KeyChar == 46)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void metroTextBoxWidth_Validating(object sender, CancelEventArgs e)
        {
            int val;
            if (int.TryParse(textBoxWidth.Text, out val))
            {
                if (val < 0 || val > 99999999)
                {
                    string infoErr = "请输入一个[0,99999999]范围内的数字";
                    errorProvider1.SetError(textBoxWidth, infoErr);
                }
                else
                {                   
                    errorProvider1.SetError(textBoxWidth, "");
                    if(_value.Width != val)
                    {
                        _value.Width = val;
                        if (_bSelfRestraintScale && _scaleSize.Width != 0)
                        {
                            _value.Height = (int)(val / (float)_scaleSize.Width * (float)_scaleSize.Height);
                        }
                        Value = _value;
                    }                  
                }
            }
            else
            {
                errorProvider1.SetError(textBoxWidth, "不是合法的数字!");
                textBoxWidth.Focus();
            }
        }

        private void metroTextBoxHeight_Validating(object sender, CancelEventArgs e)
        {
            int val;
            if (int.TryParse(textBoxHeight.Text, out val))
            {
                if (val < 0 || val > 99999999)
                {
                    string infoErr = "请输入一个[0,99999999]范围内的数字";
                    errorProvider1.SetError(textBoxHeight, infoErr);
                }
                else
                {
                    errorProvider1.SetError(textBoxHeight, "");
                    if (_value.Height != val)
                    {
                        _value.Height = val;
                        if (_bSelfRestraintScale && _scaleSize.Height != 0)
                        {
                            _value.Width = (int)(val / (float)_scaleSize.Height * (float)_scaleSize.Width);
                        }
                        Value = _value;
                    }
                }
            }
            else
            {
                errorProvider1.SetError(textBoxHeight, "不是合法的数字!");
                textBoxHeight.Focus();
            }
        }

    }
}
